package com.ohuang.test_compose.page

import android.util.Log
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.paging.compose.collectAsLazyPagingItems
import coil.annotation.ExperimentalCoilApi
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.ohuang.test_compose.MainViewModel
import com.ohuang.test_compose.composeView.RefreshList
import kotlinx.coroutines.flow.collectLatest

@OptIn(ExperimentalCoilApi::class)
@ExperimentalPermissionsApi
@InternalComposeApi
@ExperimentalAnimationApi
@ExperimentalMaterialApi
@ExperimentalUnitApi
@Composable
fun PagingListPage(viewModel: MainViewModel = androidx.lifecycle.viewmodel.compose.viewModel()) {
    val viewStates = viewModel.viewStates
    val recommendData = viewStates.pageData.collectAsLazyPagingItems()

    val isRefreshing = viewStates.isRefreshing
    val listState = viewStates.listState
    RefreshList(recommendData, listState = rememberLazyListState(), isRefreshing = isRefreshing, onRefresh = {
        viewModel.fetchData()
    }) {
        items(recommendData.itemCount) {
            ListItem(name = recommendData[it])
        }
    }

}